<?php
namespace app\api\service;

use app\api\model\charge_off;
use app\api\model\order;
use app\api\model\users;
use app\BaseController;
use think\facade\Db;

class ShopverifService extends BaseController
{
    /**
     * Su ang
     * 门店核销 商品
     * @param $code
     * @param $uid
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function Shopverif($code,$uid)
    {
      $data = users::where('uid',$uid)->find();
      $data && $data = $data->toarray();
      //判断核销人是否是门店
      if ($data['type_id'] == 1){
         $order = order::where('order_no',$code)->find();
          $order && $order = $order->toarray();
          //查询订单是否是待核销状态
         if ($order['status']==8){
             //事务保障一致性
             Db::startTrans();
             try{
                 order::where('order_no',$code)->update(['status'=>9]);
                 charge_off::create(['charge_off_member_id'=>$uid,'order_no'=>$code]);
                 Db::commit();
                 return succeedHint($data,'核销成功');
             } catch (\Exception $e) {
                 Db::rollback();
                 return errorHint('未知错误');
             }
         }else{
             return errorHint('当前订单已核销');
         }
      }else{
          return errorHint('不是门店不可以核销');
      }
    }
}